﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_ExplodingAura : Buff
    {

        private int Rank1BaseDamageLow = 100;
        private int Rank1BaseDamageHigh = 150;

        private int Rank2BaseDamageLow = 150;
        private int Rank2BaseDamageHigh = 200;

        private int triggerCount = 0;

        public Spell_ExplodingAura()
        {
            minutesLeft = 4;
        }

        public void trigger(CreatureBase attackingCreatureBase)
        {
            triggerCount++;

            int DamageAmount = 0;

            if (spellIcon.Name.IndexOf("Rank 2") > -1)
                DamageAmount = Managers.GameHandler.random.Next(Rank2BaseDamageLow, Rank2BaseDamageHigh + 1);
            else
                DamageAmount = Managers.GameHandler.random.Next(Rank1BaseDamageLow, Rank1BaseDamageHigh + 1);

            DamageAmount = SkillManager.AdjustForCombat(CastingCreatureLevel, DamageAmount, CastingCreatureInt);
            
            //Calculate resists based on TOTAL damage (all 3 triggers) otherwise the resists may mitigate to much damage.
            DamageAmount = attackingCreatureBase.CalculateGetSpellDamagePerResists(DamageAmount, spellIcon.Skill);

            DamageAmount = DamageAmount / 3;

            int absorbedDamage;
            attackingCreatureBase.InflictSpellDamage(DamageAmount, CastingCharacterID, CastingCreatureGUID, spellIcon.Skill, out absorbedDamage);
            attackingCreatureBase.Update_Character();

            DamageAmount = DamageAmount - absorbedDamage;

            if (attackingCreatureBase is Character)
            {
                Character myCharacter = (Character)attackingCreatureBase;

                Message msg = new Message();
                msg.CreatureID = myCharacter.CreatureID;
                msg.Type = "spelldamaged";
                msg.Text = TargetCreatureBase.Name + "'s aura explodes on you! (" + DamageAmount.ToString() + ")";

                if (absorbedDamage != 0)
                    msg.Text = msg.Text + " ABSORBED " + absorbedDamage.ToString();

                myCharacter.AddMessage(msg);
            }
            if (TargetCreatureBase is Character && TargetCreatureBase != attackingCreatureBase)
            {
                Character targetCharacter = (Character)TargetCreatureBase;

                Message msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Type = "spelldamage";
                msg.Text = "Your aura explodes on " + attackingCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";

                if (absorbedDamage != 0)
                    msg.Text = msg.Text + " ABSORBED " + absorbedDamage.ToString();

                targetCharacter.AddMessage(msg);

                if (attackingCreatureBase.HP < 1)
                    targetCharacter.SendMessage("You have slain " + attackingCreatureBase.Name + "!", "damage");
            }

            if (attackingCreatureBase.HP < 1)
            {
                if (!(attackingCreatureBase is Character))
                    attackingCreatureBase.currentMapItem.CreaturesBases.Remove(attackingCreatureBase);
            }

            List<Character> CharsWithin8 = attackingCreatureBase.currentMapItem.getCharactersWithin8();
            foreach (Character character in CharsWithin8)
            {
                character.PlaySound("Explosion", attackingCreatureBase.MapX, attackingCreatureBase.MapY);

                if (Math.Abs(character.MapX - attackingCreatureBase.MapX) <= 4 && Math.Abs(character.MapY - attackingCreatureBase.MapY) <= 4)
                {
                    if (attackingCreatureBase.HP < 1)
                        character.AddMapItem(attackingCreatureBase.currentMapItem);
                    else
                        character.UpdateCreatureBaseHp(attackingCreatureBase);
                }
            }

            if (triggerCount > 2)
                RemoveSpell(true, true);
        }

        public override void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if (buff.spellIcon.Name == base.spellIcon.Name && buff.SpellID != SpellID)
                {
                    buff.RemoveSpell(false, false);
                }
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if ((buff.spellIcon.Name == "Exploding Aura" && buff.SpellID != SpellID) || (buff.spellIcon.Name == "Exploding Aura Rank 2" && buff.SpellID != SpellID))
                {
                    overwrittenBuff = buff;
                    buff.RemoveSpell(false, false);
                    break;
                }
            }

            minutesLeft = CastingCreatureWis / 3;

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", character.MapX, character.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }
    }
}
